﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;


namespace QuanLyDuocPham.Controllers.QuanLyHeThong.NhapXuat
{
    class NHAPHANG_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);

        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
            if (ctrl1 != null)
            {
                ctrl1.Dispose();
                ctrl1 = null;
            }
            if (ctrl2 != null)
            {
                ctrl2.Dispose();
                ctrl2 = null;
            }
            if (ctrl3 != null)
            {
                ctrl3.Dispose();
                ctrl3 = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.PHIEUNHAP_Get_List();

            var typeorder = list.OrderBy(w => w.NGAY);

            foreach (var t in typeorder)
            {
            
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_PHIEUNHAP";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.PHIEUNHAP_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "MN00000001";
            }
            else
            {
                for (int i = 1; i < rowcount; i++)
                {
                    string IDtam = "MN";
                    for (int j = 0; j < (8 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.PHIEUNHAP_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_PHIEUNHAP.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(ComboBox cmbKho, string ma_nhap, ComboBox cmbNhanVien, ComboBox cmbNCC, TextBox txtMa_Lo, DatePicker dtpNgay, CheckBox chbCo_HD, TextBox txtTL_CK, DatePicker dtpHan_TToan, TextBox txtDa_TToan)
        {
            if (validateData(cmbKho, cmbNhanVien, cmbNCC) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmbNhanVienSelected = (NHANVIEN_Get_ListResult)cmbNhanVien.SelectedItem;
                var cmbNCCSelected = (NCC_Get_ListResult)cmbNCC.SelectedItem;
                var cmbKhoSelected = (KHO_Get_ListResult)cmbKho.SelectedItem;
                db.PHIEUNHAP_Insert( ma_nhap, cmbNhanVienSelected.MA_NHANVIEN, cmbNCCSelected.MA_NCC, cmbKhoSelected.MA_KHO, txtMa_Lo.Text.Trim(), dtpNgay.SelectedDate, chbCo_HD.IsChecked, double.Parse(txtTL_CK.Text), dtpHan_TToan.SelectedDate, int.Parse(txtDa_TToan.Text), false);
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(ComboBox cmbKho, ComboBox cmbNhanVien, ComboBox cmbNCC)
        {
            if (cmbKho.SelectedItem == null || cmbNhanVien.SelectedItem == null || cmbNCC.SelectedItem == null)
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.PHIEUNHAP_Get_List();
            var typeorder = list.OrderBy(w => w.NGAY);

            foreach (var t in typeorder)
            {
                if (t.DA_XOA == false)
                    List.Items.Add(t);
            }
        }

        public void loadData(DataPager dp)
        {
            
            var list = db.PHIEUNHAP_Get_List();
            var typeorder = list.OrderBy(w => w.NGAY);


            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {

                dpSource.Add(new { STT = i, t.CO_HD, t.DA_TTOAN, t.DA_XOA, t.HAN_TTOAN, t.MA_KHO, t.MA_LO, t.MA_NCC, t.MA_NHANVIEN, t.MA_PHIEUNHAP, t.NGAY, t.TL_CK });
                i++;

            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(ComboBox cmbKho, string ma_kho, string ma_nhap, ComboBox cmbNhanVien, ComboBox cmbNCC, DatePicker dtpNgay, CheckBox chbCo_HD, TextBox txtTL_CK, DatePicker dtpHan_TToan, TextBox txtDa_TToan)
        {
            if (validateData(cmbKho, cmbNhanVien, cmbNCC) == false)

                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {

                var cmbNhanVienSelected = (NHANVIEN_Get_ListResult)cmbNhanVien.SelectedItem;
                var cmbNCCSelected = (NCC_Get_ListResult)cmbNCC.SelectedItem;
                var cmbKhoSelected = (KHO_Get_ListResult)cmbKho.SelectedItem;
                db.PHIEUNHAP_Update(cmbKhoSelected.MA_KHO,ma_kho, ma_nhap, ma_nhap, cmbNhanVienSelected.MA_NHANVIEN, cmbNCCSelected.MA_NCC, dtpNgay.SelectedDate, chbCo_HD.IsChecked, double.Parse(txtTL_CK.Text), dtpHan_TToan.SelectedDate, int.Parse(txtDa_TToan.Text), false);
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ma_nhap, string error)
        {
            var sl = (PHIEUNHAP_GetByMaPhieunhapResult)db.PHIEUNHAP_GetByMaPhieunhap(ma_nhap).Single();

            error = "";

            if (db.PHIEUNHAP_GetByMaPhieunhap( ma_nhap).Count() > 0)
            {
                db.PHIEUNHAP_Update(ma_nhap, ma_nhap, sl.MA_NHANVIEN, sl.MA_NCC, sl.MA_KHO, sl.MA_LO, sl.NGAY, sl.CO_HD, sl.TL_CK, sl.HAN_TTOAN, sl.DA_TTOAN, true);
                error = "Xóa thành công!";
            }
            else
            {
                db.PHIEUNHAP_Delete( ma_nhap);
                error = "Xóa thành công!";

            }
            return error;

        }
        DanhMuc.NHANVIEN_Control ctrl1 = new DanhMuc.NHANVIEN_Control();
        DanhMuc.NCC_Control ctrl2 = new DanhMuc.NCC_Control();
        DanhMuc.KHO_Control ctrl3 = new DanhMuc.KHO_Control();
        public void loadForm_Edit(string ma_nhap, string ma_kho, ComboBox cmbKho, ComboBox cmbNhanVien, ComboBox cmbNCC, DatePicker dtpNgay, CheckBox chbCo_HD, TextBox txtTL_CK, DatePicker dtpHan_TToan, TextBox txtDa_TToan, CheckBox chbDa_Xoa)
        {

            var sl = (PHIEUNHAP_GetByMaPhieunhapResult)db.PHIEUNHAP_GetByMaPhieunhap( ma_nhap).Single();


            ctrl1.loadComboBox(cmbNhanVien);
            int t = 0;
            foreach (var i in cmbNhanVien.Items)
            {
                var temp = (NHANVIEN_Get_ListResult)i;
                if (temp.MA_NHANVIEN == sl.MA_NHANVIEN)
                {
                    break;
                }
                t++;
            }
            cmbNhanVien.SelectedIndex = t;

            ctrl2.loadComboBox(cmbNCC);
            int u = 0;
            foreach (var i in cmbNCC.Items)
            {
                var temp = (NCC_Get_ListResult)i;
                if (temp.MA_NCC == sl.MA_NCC)
                {
                    break;
                }
                u++;
            }
            cmbNCC.SelectedIndex = u;

            ctrl3.loadComboBox(cmbKho);
            int v = 0;
            foreach (var i in cmbKho.Items)
            {
                var temp = (KHO_Get_ListResult)i;
                if (temp.MA_KHO == sl.MA_KHO)
                {
                    break;
                }
                v++;
            }
            cmbKho.SelectedIndex = v;
            dtpNgay.SelectedDate = sl.NGAY;
            chbCo_HD.IsChecked = sl.CO_HD;
            txtTL_CK.Text = sl.TL_CK.ToString();
            
            txtDa_TToan.Text = sl.DA_TTOAN.ToString();
            dtpHan_TToan.SelectedDate = sl.HAN_TTOAN;
            chbDa_Xoa.IsChecked = sl.DA_XOA;
            
        }
    }
}
